const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
// import { WebpackBundleSizeAnalyzerPlugin } from 'webpack-bundle-size-analyzer';
var WebpackBundleSizeAnalyzerPlugin = require('webpack-bundle-size-analyzer').WebpackBundleSizeAnalyzerPlugin;

// Create multiple instances
const extractCSS = new ExtractTextPlugin('[name]-one.css');
const extractLESS = new ExtractTextPlugin('[name]-two.css');

// const css_loader = "css-loader?modules&localIdentName=[path][name]---[local]---[hash:base64:5]"
const css_loader = "css-loader?modules&minimize&localIdentName=[path][name]---[local]";//https://github.com/webpack-contrib/css-loader#minimize

const webpackConfig = {
    entry: {
        vendor: ["react"],
        app: path.resolve(__dirname, './src/index.js')
    },
    // entry: path.resolve(__dirname, './src/index.js'),//这里也可以直接写index.1.less，但是也会生成一个js文件
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'index.js'
    },
    module: {
        // webpack3.x不用loaders，用rules
        // loaders: [
        //     {
        //         test: /\.json$/,
        //         loader: 'json-loader',
        //     },
        //     {
        //         test: /\.less$/,
        //         loaders: ['style-loader', css_loader, 'less-loader'],//1.x的版本是不需要加 "-loader"的，现在用的是3.x版本，需要加
        //     },
        //     {
        //         test: /\.jsx?$/,
        //         loader: 'babel-loader',
        //         exclude: [/node_modules/, './www/comm/public'],
        //     }
        // ],

        rules: [
            {
                test: /\.less$/i,
                use: extractCSS.extract([css_loader, 'less-loader'])
            },
            {
                test: /\.jsx?$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['react']
                    }
                }
            }
        ],
    },
    devtool: "#source-map",

    plugins: [
        extractCSS, extractLESS,
        new webpack.optimize.CommonsChunkPlugin({
            name: "vendor",
            filename: "vendor.js",
            // (给 chunk 一个不同的名字)

            minChunks: Infinity,
            // (随着 entry chunk 越来越多，
            // 这个配置保证没其它的模块会打包进 vendor chunk)
        }),
        new UglifyJSPlugin({//https://doc.webpack-china.org/plugins/uglifyjs-webpack-plugin/
            sourceMap: true,//只有设置了devtool才会生效
            // mangle:false,//不混淆名字0.x要放在这里

            uglifyOptions: {
                sourceMap: true,
                ie8: false,
                ecma: 8,
                parse: {},
                mangle:false,//不混淆名字
                mangle: {
                    keep_fnames: true,
                    keep_classnames: true,
                    properties: {//https://github.com/mishoo/UglifyJS2/tree/harmony#mangle-properties-options
                        // mangle property options
                        debug: true
                    }
                },
                output: {
                    comments: false,
                    beautify: true,
                    keep_quoted_props: true,
                },
                compress: {//https://github.com/mishoo/UglifyJS2/tree/harmony#compress-options
                    unused: true,
                    dead_code: true,
                    warnings: false,
                    pure_funcs: ['console.log'],
                    drop_console: true,
                    drop_debugger: true,
                    join_vars: false,
                    collapse_vars: false,
                },
                warnings: false
            }

        }),
        new BundleAnalyzerPlugin(),
        // new WebpackBundleSizeAnalyzerPlugin(path.join(__dirname,'./reports/plain-report.txt')),
    ],
    stats: "verbose",
}


module.exports = webpackConfig;
